package com.nuyoah.common.dto;


import com.nuyoah.entity.ScoreRecord;
import com.nuyoah.entity.Statistics;
import lombok.Data;


import java.math.BigDecimal;
import java.util.List;

@Data

public class StatisticsDto {

    double average = 0;
    double highest = 0;
    double lowest = 0;


    public Statistics getAverage(Integer studentId, String type, List<ScoreRecord> scoreRecords) {
        for (ScoreRecord scoreRecord : scoreRecords) {
            average += scoreRecord.getScore();
        }
        average = average / scoreRecords.size();

        Statistics statistics = new Statistics();
        statistics.setStudentId(studentId);
        statistics.setTarget(type);
        statistics.setMetric("平均分");
        statistics.setResult(BigDecimal.valueOf(average));
        return statistics;
    }

    public Statistics getHighest(Integer studentId, String type, List<ScoreRecord> scoreRecords) {
        for (ScoreRecord scoreRecord : scoreRecords) {
            if (scoreRecord.getScore() > highest) {
                highest = scoreRecord.getScore();
            }
        }

        Statistics statistics = new Statistics();
        statistics.setStudentId(studentId);
        statistics.setTarget(type);
        statistics.setMetric("最高分");
        statistics.setResult(BigDecimal.valueOf(highest));
        return statistics;
    }

    public Statistics getLowest(Integer studentId, String type, List<ScoreRecord> scoreRecords) {
        for (ScoreRecord scoreRecord : scoreRecords) {
            if (scoreRecord.getScore() < lowest || lowest == 0) {
                lowest = scoreRecord.getScore();
            }
        }

        Statistics statistics = new Statistics();
        statistics.setStudentId(studentId);
        statistics.setTarget(type);
        statistics.setMetric("最低分");
        statistics.setResult(BigDecimal.valueOf(lowest));
        return statistics;
    }
}
